package com.jay.tools.utils.file_util;

import android.net.Uri;
import android.util.Base64;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class FileUtil {

    private static String SDPATH = SDCardUtils.getSDCardPath();

    /**
     * 在SD卡上创建文件
     *
     * @param saveFileName
     * @return
     * @throws IOException
     */
    public static File creatSDFile(String saveFileName) throws IOException {
        File file = new File(SDPATH + saveFileName);
        file.createNewFile();
        return file;
    }

    public static void saveFile(File file, byte[] data) throws Exception {
        FileOutputStream outStream = new FileOutputStream(file);
        outStream.write(data);
        outStream.close();
    }

    /**
     * @param path2 删除文件
     */
    public static void DeleteFileName(String path2) {
        String path = SDPATH + path2 + "/";
        File file = new File(path);
        if (file != null)
            deleteFile(file);
    }

    /**
     * @param file 删除文件
     */
    private static void deleteFile(File file) {
        if (file.exists() == false) {
            return;
        } else {
            if (file.isFile()) { // 是文件
                file.delete();// 删除文件
                return;
            } else if (file.isDirectory()) { // 是目录
                File[] childFile = file.listFiles();
                if (childFile == null || childFile.length == 0) {
                    file.delete();// 删除本身
                    return;
                }
                for (File f : childFile) {
                    deleteFile(f);
                }
                file.delete();// 删除本身
            }
        }
    }

    /**
     * @param dirName
     * @return 在SD卡上创建单层目录
     */
    public static File creatSDDir(String dirName) {
        File dir = new File(SDPATH + dirName);
        dir.mkdir();
        return dir;
    }

    /**
     * 判断SD卡上的文件夹是否存在
     */
    public static boolean isFileExist(String fileName) {
        return new File(SDPATH + fileName).exists();
    }

    /**
     * 判断SD卡上的文件夹是否存在
     */
    public static Uri getUri(String fileName) {
        return Uri.fromFile(new File(SDPATH + fileName));
    }

    /**
     * 由于得到一个InputStream对象是所有文件处理前必须的操作，所以将这个操作封装成了一个方法
     *
     * @param urlStr
     * @return
     * @throws IOException
     */
    public static InputStream getInputStream(String urlStr) throws IOException {
        InputStream is = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            is = urlConn.getInputStream();
            int fileSize = urlConn.getContentLength();// 根据响应获取文件大小
            if (fileSize <= 0)
                throw new RuntimeException("无法获知文件大小 ");

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return is;
    }

    /**
     * 从路径获取文件名
     *
     * @param pathandname
     * @return
     */
    public static String getFileName(String pathandname) {
        int start = pathandname.lastIndexOf("/");
        int end = pathandname.lastIndexOf(".");
        if (start != -1 && end != -1) {
            return pathandname.substring(start + 1, end);
        } else {
            return null;
        }
    }

    /**
     * 通过路径生成Base64文件
     *
     * @param path
     * @return
     */
    public static String getBase64FromPath(String path) {
        String base64 = "";
        try {
            File file = new File(path);
            byte[] buffer = new byte[(int) file.length() + 100];
            @SuppressWarnings("resource")
            int length = new FileInputStream(file).read(buffer);
            base64 = Base64.encodeToString(buffer, 0, length, Base64.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return base64;
    }

}
